package com.unsins.restlet;

import com.unsins.business.domain.system.SysConstant;
import org.compass.core.Compass;
import org.compass.core.CompassHit;
import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchHelper;
import org.compass.core.support.search.CompassSearchResults;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.Context;
import org.restlet.data.*;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: odpsoft
 * Date: 2008-11-28
 * Time: 9:54:45
 */
public class SysConstantSearcher extends Resource implements InitializingBean {
    private CompassSearchHelper searchHelper;
    private static final Logger logger = Logger.getLogger(SysConstantResource.class.getName());
    private Compass compass;
    private static final Integer MAX_PAGED = 12;
    private static final String URL_ENCODING = "ISO-8859-1";
    private static final String TARGET_ENCODING = "UTF-8";

    public void setCompass(Compass cps) {
        compass = cps;
    }

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(compass, "compass is required.but it is null!");
        if (searchHelper == null) {
            searchHelper = new CompassSearchHelper(compass, MAX_PAGED);
        }
    }

    @Override
    public void init(Context context, Request request, Response response) {
        super.init(context, request, response);
        getVariants().add(new Variant(MediaType.TEXT_PLAIN));
    }

    @Override
    public Representation represent(Variant variant) throws ResourceException {
        Form request = getRequest().getResourceRef().getQueryAsForm();
        String qKeyword = request.getFirstValue("q");
        String startPage = request.getFirstValue("start");
        String keyWord = "";
        try {
            keyWord = new String(qKeyword.getBytes(URL_ENCODING), TARGET_ENCODING);
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

        CompassSearchCommand command = new CompassSearchCommand();
        command.setPage(new Integer(startPage));
        command.setQuery("+sysconstant.typeName:" + keyWord);
        logger.info(command.getQuery());

        CompassSearchResults results = searchHelper.search(command);

        CompassHit[] hits = results.getHits();
        JSONArray array = new JSONArray();
        try {
            for (CompassHit hit : hits) {
                SysConstant constant = (SysConstant) hit.data();
                JSONObject json = new JSONObject();

                json.put("name", constant.getTypeName());
                json.put("code", constant.getListCode());
                json.put("id", constant.getId());
                array.put(json);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return new StringRepresentation(array.toString(), MediaType.TEXT_PLAIN, Language.ALL, CharacterSet.UTF_8);
    }
}
